$(function () {
    //供应商编码验证
    $('#proCode').blur(checkProCode)
    //供应商名称验证
    $('#proName').blur(checkProName)
    //联系人验证
    $('#proContact').blur(checkProContact)
    // 联系电话验证
    $('#proPhone').blur(checkProPhone)
    //联系地址验证
    $('#proAddress').blur(checkProAddress)
    //传真验证
    $('#proFax').blur(checkProFax)
    //描述验证
    $('#proDesc').blur(checkProDesc)
    //表单提交
    $('.addForm').submit(async function (e) {
        e.preventDefault()
        //判断当前的表单验证是否通过
        let f1 = await checkProCode()
        let f2 = await checkProName()
        let f3 = checkProContact()
        let f4 = await checkProPhone()
        let f5 = checkProAddress()
        let f6 = checkProFax()
        let f7 = checkProDesc()
        let flag = f1 & f2 & f3 & f4 & f5 & f6 & f7
        if (flag === 0) {
            return alert('请正确填写信息')
        }
        //提交表单进行增加供应商操作
        addProvider()
    })
})


/**
 * 新增供应商信息的函数
 * @date 2021-05-10
 * @returns {any}
 */
async function addProvider() {
    //准备参数
    let options = {
        url: 'http://146.56.230.35:3007/provider/addProvider',
        type: 'POST',
        data: {
            proCode: $('#proCode').val(),
            proName: $('#proName').val(),
            proContact: $('#proContact').val(),
            proPhone: $('#proPhone').val(),
            proAddress: $('#proAddress').val(),
            proFax: $('#proFax').val(),
            proDesc: $('#proDesc').val(),
        },
    }
    //发起请求
    let res = await request(options)
    console.log(res)
    if (res.code !== 200) {
        return alert(res.msg)
    }
    //新增用户成功，跳转到用户列表页面
    location = 'providerList.html'
}


/**
 * 供应商编码验证
 * @date 2021-05-10
 * @returns {any}
 */
async function checkProCode() {
    let ProCode = $('#proCode')
    ProCode[0].className = ''
    let v = ProCode.val()
    let div = ProCode.parent()
    let span = ProCode.next()
    span.empty()
    //判断空
    if (!v) {
        div.removeClass('ok').addClass('error')
        span.html('供应商编码不能为空')
        return false
    }
    //数字字母下划线6-12位
    let reg = /^[$\w]{6,12}$/
    if (!reg.test(v)) {
        div.removeClass('ok').addClass('error')
        span.html('供应商编码由数字、字母、_、$组成6-12位')
        return false
    }
    //验证唯一
    let flag = await getProviderByProCode(v)
    if (flag) {
        div.removeClass('ok').addClass('error')
        span.html('供应商编码被占用了')
        return false
    }
    div.removeClass('error').addClass('ok')
    span.html('供应商编码正确')
    return true
}


/**
 * 供应商名称验证
 * @date 2021-05-10
 * @returns {any}
 */
async function checkProName() {
    let ProName = $('#proName')
    ProName[0].className = ''
    let v = ProName.val()
    let div = ProName.parent()
    let span = ProName.next()
    span.empty()
    //判断空
    if (!v) {
        div[0].className = 'error'
        span.html('供应商名称不能为空')
        return false
    }
    //全英文或全汉字2-12位
    let reg = /^([\u4e00-\u9fa5]{2,12})$|^([a-zA-Z]{2,12})$/
    if (!reg.test(v)) {
        div[0].className = 'error'
        span.html('供应商名称为全英文或全汉字2-12位')
        return false
    }
    //验证唯一
    let flag = await getProviderByProName(v)
    if (flag) {
        div[0].className = 'error'
        span.html('供应商名称被占用了')
        return false
    }
    div[0].className = 'ok'
    span.html('供应商名称正确')
    return true
}

/**
 * 联系人验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProContact() {
    let ProContact = $('#proContact')
    ProContact[0].className = ''
    let v = ProContact.val()
    let div = ProContact.parent()
    let span = ProContact.next()
    span.empty()
    //判断空
    if (!v) {
        div[0].className = 'error'
        span.html('联系人不能为空')
        return false
    }
    //全英文或全汉字2-12位
    let reg = /^([\u4e00-\u9fa5]{2,12})$|^([a-zA-Z]{2,12})$/
    if (!reg.test(v)) {
        div[0].className = 'error'
        span.html('联系人为全英文或全汉字2-12位')
        return false
    }
    div[0].className = 'ok'
    span.html('联系人正确')
    return true
}


/**
 * 联系电话验证
 * @date 2021-05-10
 * @returns {any}
 */
async function checkProPhone() {
    let ProPhone = $('#proPhone')
    let v = ProPhone.val().trim()
    let parent = ProPhone.parent()
    let span = ProPhone.next()
    span.empty()
    //判断非空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('电话不能空')
        return false
    }
    //规则
    let reg = /^1[3456789]\d{9}$/
    if (!reg.test(v)) {
        parent.removeClass('ok').addClass('error')
        span.html('电话号码非法')
        return false
    }
    // 唯一
    let flag = await getProviderByProPhone(v)
    if (flag) {
        parent.removeClass('ok').addClass('error')
        span.html('电话号码被占用')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('电话正确')
    return true
}


/**
 * 联系地址验证
 * @date 2021-05-10
 * @returns {any}
 */
function checkProAddress() {
    let ProAddress = $('#proAddress')
    let v = ProAddress.val().trim()
    let parent = ProAddress.parent()
    let span = ProAddress.next()
    span.empty()
    //判断非空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('地址不能空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('地址正确')
    return true
}

/**
 * 传真验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProFax() {
    let ProFax = $('#proFax')
    let v = ProFax.val().trim()
    let parent = ProFax.parent()
    let span = ProFax.next()
    span.empty()
    //判断非空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('地址不能空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('地址正确')
    return true
}


/**
 * 描述验证
 * @date 2021-05-11
 * @returns {any}
 */
function checkProDesc() {
    let ProDesc = $('#proDesc')
    let v = ProDesc.val().trim()
    let parent = ProDesc.parent()
    let span = ProDesc.next()
    span.empty()
    //判断非空
    if (!v) {
        parent.removeClass('ok').addClass('error')
        span.html('地址不能空')
        return false
    }
    parent.removeClass('error').addClass('ok')
    span.html('地址正确')
    return true
}

//通过供应商编码获得供应商信息 返回0表示供应商编码可用，返回1表示不可用
async function getProviderByProCode(v) {
    let options = {
        url: 'http://146.56.230.35:3007/provider/getProviderByProCode',
        type: 'GET',
        data: {
            proCode: v
        },
    }
    let res = await request(options)
    console.log(res)
    if (res.code !== 200) {
        return 0
    }
    if (res.user) {
        return 1
    }
    return 0
}

//通过供应商名称获得供应商信息 返回0表示供应商名可用，返回1表示不可用
async function getProviderByProName(v) {
    let options = {
        url: 'http://146.56.230.35:3007/provider/getProviderByProName',
        type: 'GET',
        data: {
            proName: v
        },
    }
    let res = await request(options)
    console.log(res)
    if (res.code !== 200) {
        return 0
    }
    if (res.user) {
        return 1
    }
    return 0
}

//通过联系电话获得h供应商信息，返回0表示电话可用，返回1表示不可用
async function getProviderByProPhone(v) {
    let options = {
        url: 'http://146.56.230.35:3007/provider/getProviderByProPhone',
        type: 'GET',
        data: {
            proPhone: v
        },
    }
    let res = await request(options)
    console.log(res)
    if (res.code !== 200) {
        return 0
    }
    if (res.user) {
        return 1
    }
    return 0
}